package com.oa.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.oa.bean.work.Appliance;
import com.oa.bean.work.Atype;
import com.oa.bean.work.Dept;
import com.oa.bean.work.Emp;
import com.oa.dao.WorkDAO;

public class WorkDAOImpl implements WorkDAO {
	
	private SessionFactory sessionfactory;
	
	private SessionFactory sessionFactory;
	
	private Session session;
	

	public void setSessionfactory(SessionFactory sessionfactory) {
		this.sessionfactory = sessionfactory;
	}
	
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	@Override
	public Atype atypeSave(Atype atype) {//添加办公用品类型
		Session session = sessionFactory.getCurrentSession();
		session.beginTransaction();
		session.save(atype);
		session.getTransaction().commit();
		return atype;
	}

	@Override
	public void atypeUpdate(Atype atype) {
		Session session = sessionFactory.getCurrentSession();
		session.beginTransaction();
		session.saveOrUpdate(atype);
		session.getTransaction().commit();
	}

	@Override
	public void atypeDelete(Atype atype) {
		Session session = sessionFactory.getCurrentSession();
		session.beginTransaction();
		session.delete(atype);
		session.getTransaction().commit();
	}

	@Override
	public Appliance applianceSave(Appliance appliance) {//添加办公用品
		Session session = sessionFactory.getCurrentSession();
		session.beginTransaction();
		session.save(appliance);
		session.getTransaction().commit();
		return appliance;
	}

	@Override
	public void applianceUpdate(Appliance appliance) {
		Session session = sessionFactory.getCurrentSession();
		session.beginTransaction();
		session.saveOrUpdate(appliance);
		session.getTransaction().commit();
	}

	@Override
	public void applianceDelete(Appliance appliance) {
		Session session = sessionFactory.getCurrentSession();
		session.beginTransaction();
		session.delete(appliance);
		session.getTransaction().commit();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Appliance> applianceQueryPager(int page, int pageSize) {
		Session session = sessionFactory.openSession();
		Criteria c = session.createCriteria(Appliance.class);
		c.setFirstResult((page - 1) * pageSize);// 设置从哪里开始查询，0开始第一条
		c.setMaxResults(pageSize);// 设置查询的total(数量)
		List<Appliance> list = c.list();
		return list;
	}

	@Override
	public void deptSave(Dept dept) {
		Session session = sessionfactory.getCurrentSession();
		Transaction tr = session.beginTransaction();
		session.save(dept);
		tr.commit();
	}

	@Override
	public Dept deptUpdate(Dept dept) {
		Session session = sessionfactory.getCurrentSession();
		Transaction tr = session.beginTransaction();
		session.saveOrUpdate(dept);
		tr.commit();
		return dept;
	}

	@Override
	public void deptDelete(int id) {
		Session session = sessionfactory.getCurrentSession();
		Transaction tr = session.beginTransaction();
		Dept d = new Dept();
		d.setDid(id);
		session.delete(d);
		tr.commit();
	}

	@SuppressWarnings("unchecked")
	@Override/**分页查询所有部门信息*/
	public List<Dept> deptQueryByPage(int pageNo, int pageSize) {
		session = sessionfactory.openSession();
		Criteria criteria = session.createCriteria(Dept.class);
		criteria.setFirstResult((pageNo - 1)*pageSize);
		criteria.setMaxResults(pageSize);
		List<Dept> list = criteria.list();
		if (list != null)
		System.out.println(list);
		return list;
	}

	@Override
	public void empSave(Emp emp) {
		Session session = sessionfactory.getCurrentSession();
		Transaction tr = session.beginTransaction();
		session.save(emp);
		tr.commit();
	}

	@Override
	public Emp empUpdate(Emp emp) {
		Session session = sessionfactory.getCurrentSession();
		Transaction tr = session.beginTransaction();
		session.saveOrUpdate(emp);
		tr.commit();
		return emp;
	}

	@Override
	public void empDelete(int id) {
		Session session = sessionfactory.getCurrentSession();
		Transaction tr = session.beginTransaction();
		Emp e = new Emp();
		e.setEid(id);
		session.delete(e);
		tr.commit();
	}

	@SuppressWarnings("unchecked")
	@Override/**分页查询所有员工信息*/
	public List<Emp> empQueryPage(int pageNo, int pageSize) {
		session = sessionfactory.openSession();
		Criteria criteria = session.createCriteria(Emp.class);
		criteria.setFirstResult((pageNo-1)*pageSize);
		criteria.setMaxResults(pageSize);
		List<Emp> list = criteria.list();
		return list;
	}

	@Override
	public void getClose() {
		session.close();
	}

}
